package org.roguelikedevelopment.dweller.j2me;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

import org.roguelikedevelopment.dweller.common.application.DataStorage;
import org.roguelikedevelopment.dweller.common.util.StorableData;
import org.roguelikedevelopment.dweller.common.util.compression.RangeEncoding;
import org.roguelikedevelopment.dweller.common.util.logger.Logger;

public class RecordStoreStorage implements DataStorage {

	public void store(String name, StorableData data, boolean compress) {
		store(name, data.getWrittenBytes(), compress);
	}
	
	private void store(String name, byte[] data, boolean compress) {
		Logger.debug("DwellerMidlet.save() uncompressed data length " + data.length);
		if(compress) {
			System.gc();
			data = RangeEncoding.compress(data);
			System.gc();
			Logger.debug("DwellerMidlet.save() compressed data length " + data.length);
		}
		RecordStore recordStore = null;
		try {
			recordStore = RecordStore.openRecordStore(name, true);
			if(recordStore.getNumRecords() == 0) {
				recordStore.addRecord(data, 0, data.length);
			}
			else {
				recordStore.setRecord(1, data, 0, data.length);
			}
		}
		catch(Exception e) {
			Logger.error("Unable to write to RS " + e.getMessage());
		}
		finally {
			if(recordStore != null) {
				try {
					recordStore.closeRecordStore();
				}
				catch(Exception e) {
				}
			}
		}
	}

	
	
	public StorableData load(String name, boolean decompress) {
		byte record[] = null;
		RecordStore recordStore = null;
		try {
			recordStore = RecordStore.openRecordStore(name, true);
			record = recordStore.getRecord(1);
		}
		catch(Exception e) {
			Logger.error("Unable to load from RS " + e.getMessage());
			return null;
		}
		finally {
			if(recordStore != null) {
				try {
					recordStore.closeRecordStore();
				}
				catch(Exception e) {
				}
			}
		}
		if(decompress) {
			System.gc();
			record = RangeEncoding.decompress(record);
			System.gc();
		}
		return new StorableData(record);
	}
	
	

	public boolean exists(String name) {
		RecordStore recordStore = null;
		try {
			recordStore = RecordStore.openRecordStore(name, true);
			return (recordStore.getNumRecords() == 1);
		}
		catch(Exception e) {
			Logger.error("Unable to check RS " + e.getMessage());
			return false;
		}
		finally {
			if(recordStore != null) {
				try {
					recordStore.closeRecordStore();
				}
				catch(Exception e) {
				}
			}
		}
	}



	public boolean delete(String name) {
		try {
			RecordStore.deleteRecordStore(name);
		}
		catch(Exception e) {
			Logger.error("Unable to delete RS " + e.getMessage());
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public long getAvailableSize() {
		String[] list = getDataList();
		if(list == null) {
			return -1;
		}
		long size = -1;
		try {
			RecordStore rs = RecordStore.openRecordStore(list[0], true);
			size = rs.getSizeAvailable();
			rs.closeRecordStore();
		}
		catch(RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return size;
	}

	public long getSize(String name) {
		long size = -1;
		try {
			RecordStore rs = RecordStore.openRecordStore(name, true);
			size = rs.getSize();
			rs.closeRecordStore();
		}
		catch(RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return size;
	}

	public String[] getDataList() {
		return RecordStore.listRecordStores();
	}

}
